package com.lanlion.portlet;

import com.alibaba.fastjson.JSONObject;
import com.lanlion.porlet.common.*;
import com.lanlion.portlet.http.HttpHelper;
import com.lanlion.portlet.http.HttpResult;
import com.lanlion.portlet.http.HttpSSLHelper;
import com.lanlion.portlet.tool.StringHelper;
import com.lanlion.portlet.tool.WechatConstants;
import com.lanlion.portlet.tool.WechatEncryptHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 微信服务商模式工具类
 */
public class WechatSPMUtils {

    static Logger logger = LoggerFactory.getLogger(WechatSPMUtils.class);
    //不带证书请求工具类
    static HttpHelper httpHelper = HttpHelper.getHttpFactory();


    /**
     * 统一下单API
     * @param payEntity 统一下单请求实体
     * @param key 支付秘钥
     * @return wechatPayView
     * @remark H5和JSAPI通用接口，详细必传参数和返回结果可参考官方链接
     */
    public static WechatPayView getUnifiedorderId(WechatPayEntity payEntity,String key)throws Exception{
        WechatPayView wechatPayView = null;
        String url = WechatConstants.UNIFIEDORDER;
        Map params = WechatEncryptHelper.getSignFromEntity(payEntity,key); //获取加密后的参数MAP
        HttpResult res = httpHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            wechatPayView = new WechatPayView();
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),wechatPayView);//将请求结果转换为实体类
        }
        return wechatPayView;
    }


    /**
     * 微信支付订单状态查询
     * @param payEntity
     * @param key 支付秘钥
     * @return
     * @throws Exception
     * @remark 加密方式默认MD5，且不用传参数
     *
     */
    public static WechatPayView getOrderStatus(WechatPayEntity payEntity,String key)throws Exception{
        WechatPayView wechatPayView = null;
        String url = WechatConstants.ORDER_QUERY;
        Map params = WechatEncryptHelper.getSignFromEntity(payEntity,key); //获取加密后的参数MAP
        HttpResult res = httpHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            wechatPayView = new WechatPayView();
            logger.info(res.getResult());
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),wechatPayView);//将请求结果转换为实体类
        }
        return wechatPayView;
    }



    /**
     * 申请退款
     * @param payEntity
     * @param key 支付秘钥
     * @param cert 证书路径
     * @param certKey 证书秘钥
     * @return
     * @throws Exception
     */
    public static WechatPayView refund(WechatPayEntity payEntity,String key,String cert,String certKey)throws Exception{
        WechatPayView wechatPayView = null;
        String url = WechatConstants.REFUND;
        payEntity.setSignType(WechatConstants.HMACSHA256); //设置加密方式
        Map params = WechatEncryptHelper.getSignFromEntity(payEntity,key); //获取加密后的参数MAP
        HttpSSLHelper httpSSLHelper = new HttpSSLHelper(cert,certKey);
        HttpResult res = httpSSLHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            wechatPayView = new WechatPayView();
            logger.info(res.getResult());
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),wechatPayView);//将请求结果转换为实体类
        }
        return wechatPayView;
    }


    /**
     *  退款查询
     * @param payEntity
     * @param key
     * @return
     * @throws Exception
     */
    public static WechatPayView refundQuery(WechatPayEntity payEntity,String key)throws Exception{
        WechatPayView wechatPayView = null;
        String url = WechatConstants.REFUND_QUERY;
        Map params = WechatEncryptHelper.getSignFromEntity(payEntity,key); //获取加密后的参数MAP
        HttpResult res = httpHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            wechatPayView = new WechatPayView();
            logger.info(res.getResult());
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),wechatPayView);//将请求结果转换为实体类
        }
        return wechatPayView;
    }


    /**
     * 微信请求添加分账方
     * @param shareFundsEntity
     * @param key
     * @param cert
     * @param certKey
     * @return
     * @throws Exception
     */
    public static WechatShareFundsView addSharin(WechatShareFundsEntity shareFundsEntity,String key,String cert,String certKey)throws Exception{

        WechatShareFundsView wechatShareFundsView = null;
        String url = WechatConstants.PROFITSHARINGADDRECEIVER;
        shareFundsEntity.setSignType(WechatConstants.HMACSHA256); //设置加密方式
        HttpSSLHelper httpSSLHelper = new HttpSSLHelper(cert,certKey);
        Map params = WechatEncryptHelper.getSignFromEntity(shareFundsEntity,key); //获取加密后的参数MAP
        System.out.println(params);
        System.out.println(StringHelper.mapToXml(params));
        HttpResult res = httpSSLHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            wechatShareFundsView = new WechatShareFundsView();
            logger.info(res.getResult());
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),wechatShareFundsView);//将请求结果转换为实体类
        }
        return wechatShareFundsView;
    }



    /**
     * 微信请求单词分账
     * @param shareFundsEntity
     * @param key 支付秘钥
     * @return
     * @throws Exception
     */
    public static WechatShareFundsView profitSharin(WechatShareFundsEntity shareFundsEntity,String key,String cert,String certKey)throws Exception{

        WechatShareFundsView wechatShareFundsView = null;
        String url = WechatConstants.PROFITSHARING;
        shareFundsEntity.setSignType(WechatConstants.HMACSHA256); //设置加密方式
        HttpSSLHelper httpSSLHelper = new HttpSSLHelper(cert,certKey);
        Map params = WechatEncryptHelper.getSignFromEntity(shareFundsEntity,key); //获取加密后的参数MAP
        System.out.println(params);
        System.out.println(StringHelper.mapToXml(params));
        HttpResult res = httpSSLHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            wechatShareFundsView = new WechatShareFundsView();
            logger.info(res.getResult());
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),wechatShareFundsView);//将请求结果转换为实体类
        }
        return wechatShareFundsView;
    }


    /**
     * 完结分账
     * @param shareFundsEntity
     * @param key
     * @param cert 证书路径
     * @param certKey 证书秘钥
     * @return
     * @throws Exception
     */
    public static WechatShareFundsView finshProfitSharin(WechatShareFundsEntity shareFundsEntity,String key,String cert,String certKey)throws Exception{

        WechatShareFundsView wechatShareFundsView = null;
        String url = WechatConstants.PROFITSHARINGFINISH;
        shareFundsEntity.setSignType(WechatConstants.HMACSHA256); //设置加密方式
        HttpSSLHelper httpSSLHelper = new HttpSSLHelper(cert,certKey);
        Map params = WechatEncryptHelper.getSignFromEntity(shareFundsEntity,key); //获取加密后的参数MAP
        HttpResult res = httpSSLHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            wechatShareFundsView = new WechatShareFundsView();
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),wechatShareFundsView);//将请求结果转换为实体类
        }
        return wechatShareFundsView;
    }


    /**
     *
     * @param payEntity
     * @param key
     * @return
     * @throws Exception
     */
    public static WechatShareFundsView profitSharinQuery(WechatPayEntity payEntity,String key)throws Exception{
        WechatShareFundsView wechatShareFundsView = null;
        String url = WechatConstants.PROFITSHARINGQUERY;
        payEntity.setSignType(WechatConstants.HMACSHA256);
        Map params = WechatEncryptHelper.getSignFromEntity(payEntity,key); //获取加密后的参数MAP
        HttpResult res = httpHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            wechatShareFundsView = new WechatShareFundsView();
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),wechatShareFundsView);//将请求结果转换为实体类
        }
        return wechatShareFundsView;
    }

    /**
     * 企业付款到个人零钱
     * @param key
     * @param cert
     * @param certKey
     * @return
     * @throws Exception
     */
    public static Pay2PersonFromView pay2PersonFromCompany(Pay2PersonFromCompanyEntity pay2PersonFromCompanyEntity, String key, String cert, String certKey)throws Exception{

        Pay2PersonFromView pay2PersonFromView = null;
        String url = WechatConstants.PAY2PERSON_FROM_COMPANY;
        HttpSSLHelper httpSSLHelper = new HttpSSLHelper(cert,certKey);
        Map params = WechatEncryptHelper.getSignFromEntity(pay2PersonFromCompanyEntity,key); //获取加密后的参数MAP
        HttpResult res = httpSSLHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            pay2PersonFromView = new Pay2PersonFromView();
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),pay2PersonFromView);//将请求结果转换为实体类
        }
        return pay2PersonFromView;
    }


    /**
     * 查询企业付款状态
     * @param pay2PersonFromCompanyEntity
     * @param key
     * @param cert
     * @param certKey
     * @return
     * @throws Exception
     */
    public static Pay2PersonFromView getPay2PersonFromCompanyStatus(Pay2PersonFromCompanyEntity pay2PersonFromCompanyEntity, String key, String cert, String certKey)throws Exception{

        Pay2PersonFromView pay2PersonFromView = null;
        String url = WechatConstants.PAY2PERSON_FROM_COMPANY_STATUS;
        HttpSSLHelper httpSSLHelper = new HttpSSLHelper(cert,certKey);
        Map params = WechatEncryptHelper.getSignFromEntity(pay2PersonFromCompanyEntity,key); //获取加密后的参数MAP
        HttpResult res = httpSSLHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            pay2PersonFromView = new Pay2PersonFromView();
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),pay2PersonFromView);//将请求结果转换为实体类
        }
        return pay2PersonFromView;
    }

    /**
     * 发放普通微信红包
     * @param redPackEntity
     * @param key
     * @param cert
     * @param certKey
     * @return
     * @throws Exception
     */
    public static WechatRedPackView sendNormalRedPackage(WechatRedPackEntity redPackEntity, String key, String cert, String certKey)throws Exception{

        WechatRedPackView redPackView = null;
        String url = WechatConstants.SEND_RED_PACKAGE;
        HttpSSLHelper httpSSLHelper = new HttpSSLHelper(cert,certKey);
        Map params = WechatEncryptHelper.getSignFromEntity(redPackEntity,key); //获取加密后的参数MAP
        HttpResult res = httpSSLHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            redPackView = new WechatRedPackView();
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),redPackView);//将请求结果转换为实体类
        }
        return redPackView;
    }


    /**
     * 发放裂变红包，主要有一个amt_type参数需要设置
     * @param redPackEntity
     * @param key
     * @param cert
     * @param certKey
     * @return
     * @throws Exception
     */
    public static WechatRedPackView sendGroupRedPackage(WechatRedPackEntity redPackEntity, String key, String cert, String certKey)throws Exception{

        WechatRedPackView redPackView = null;
        String url = WechatConstants.SEND_GROUP_RED_PACKAGE;
        HttpSSLHelper httpSSLHelper = new HttpSSLHelper(cert,certKey);
        Map params = WechatEncryptHelper.getSignFromEntity(redPackEntity,key); //获取加密后的参数MAP
        HttpResult res = httpSSLHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            redPackView = new WechatRedPackView();
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),redPackView);//将请求结果转换为实体类
        }
        return redPackView;
    }


    /**
     * 红包发送查询
     * @param redPackEntity
     * @param key
     * @param cert
     * @param certKey
     * @return
     * @throws Exception
     */
    public static WechatRedPackView getRedPackageInfo(WechatRedPackEntity redPackEntity, String key, String cert, String certKey)throws Exception{

        WechatRedPackView redPackView = null;
        String url = WechatConstants.GET_RED_PACKAGE_INFO;
        HttpSSLHelper httpSSLHelper = new HttpSSLHelper(cert,certKey);
        Map params = WechatEncryptHelper.getSignFromEntity(redPackEntity,key); //获取加密后的参数MAP
        HttpResult res = httpSSLHelper.doPost4XML(url,StringHelper.mapToXml(params));
        if(res.getStatusCode()==WechatConstants.HTTP_OK){
            StringHelper.xmlStringToAnnotationEntity(res.getResult(),redPackView);//将请求结果转换为实体类
        }
        return redPackView;
    }

    public static void main(String[] args) {
        String xmlString ="<xml><return_code><![CDATA[SUCCESS]]></return_code>\n" +
                "<return_msg><![CDATA[OK]]></return_msg>\n" +
                "<appid><![CDATA[wx19a9b0fc9cd844ec]]></appid>\n" +
                "<mch_id><![CDATA[1560274011]]></mch_id>\n" +
                "<sub_mch_id><![CDATA[1563161861]]></sub_mch_id>\n" +
                "<nonce_str><![CDATA[piJdhOc2KpvzeGX6]]></nonce_str>\n" +
                "<sign><![CDATA[9221798EF28BE82040441A6AA56F20DA5F801726C23294868953EF88B37018E3]]></sign>\n" +
                "<result_code><![CDATA[SUCCESS]]></result_code>\n" +
                "<prepay_id><![CDATA[wx15195016810771eba6cb51181724013300]]></prepay_id>\n" +
                "<trade_type><![CDATA[JSAPI]]></trade_type>\n" +
                "<sub_appid><![CDATA[wx6e4a8f6c7acd4906]]></sub_appid>\n" +
                "</xml>";
        WechatPayView view = new  WechatPayView();
        try {
             StringHelper.xmlStringToAnnotationEntity(xmlString,view);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(JSONObject.toJSONString(view));
    }
}
